﻿using System;
using System.Collections.Generic;
using iTextSharp.text;
using iTextSharp.text.pdf;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.IO;
using O2S.Components.PDFRender4NET;
using System.Drawing;
using drawing_print.configure;
using System.Drawing.Imaging;

namespace drawing_print.util
{
    public class Pdf
    {
        public static List<string> splitPdf(string sourPdfPath, string outputPath, int totalPageNum)
        {
            // 多余页
            int dyPage = totalPageNum % 10;
            // pdf 个数
            int pdfNum = totalPageNum / 10 + (dyPage > 0 ? 1 : 0);
            List<string> list = new List<string>();
            for (int i = 0; i < pdfNum; i++)
            {
                string outPath = Path.Combine(outputPath, "Split" + (i + 1) + ".pdf");
                list.Add(outPath);
                if (i + 1 == pdfNum)
                {
                    // 最后一页两种情况 
                    if(dyPage == 0)
                    {
                        // 20
                        extractPages(sourPdfPath, outPath, (i * 10) + 1, (i + 1) * 10);
                    }
                    else
                    {
                        // 15
                        extractPages(sourPdfPath, outPath, (i * 10) + 1, i * 10 + dyPage);
                    }



                }
                else
                {
                    extractPages(sourPdfPath, outPath, (i * 10) + 1, i * 10 + 10);
                }

            }
            return list;
        }

        public static void extractPages(string sourcePdfPath, string outputPdfPath, int startPage, int endPage)
        {
            PdfReader reader = null;
            Document sourceDocument = null;
            PdfCopy pdfCopyProvider = null;
            PdfImportedPage importedPage = null;
            try
            {
                reader = new PdfReader(sourcePdfPath);
                sourceDocument = new Document(reader.GetPageSizeWithRotation(startPage)); pdfCopyProvider = new PdfCopy(sourceDocument, new System.IO.FileStream(outputPdfPath, System.IO.FileMode.Create));
                sourceDocument.Open();
                for (int i = startPage; i <= endPage; i++)
                {
                    importedPage = pdfCopyProvider.GetImportedPage(reader, i); pdfCopyProvider.AddPage(importedPage);
                }
                
            }
            catch (Exception ex)
            {
                throw ex;
            }
            finally
            {
                if (sourceDocument != null)
                {
                    sourceDocument.Close();
                }
                
                if (reader != null)
                {
                    reader.Close();
                }
                
            }
        }
        // -----------------------------------PDF转图片-------------------------------------
        /// <summary>
        /// 
        /// </summary>
        /// <param name="pdfPath"></param>
        public static Boolean PdfToImage(string pdfPath)
        {
            try
            {
                using (PDFFile pdfFile = PDFFile.Open(pdfPath))
                {
                    for (int i = 0; i < pdfFile.PageCount; i++)
                    {
                        Bitmap pageImage = pdfFile.GetPageImage(i, 56 * (int)Definition.Two);
                        string fileName = string.Format("{0}\\{1}{2}", AppConfig.ImagePath, i + 1, ".png");
                        Console.WriteLine(fileName);
                        pageImage.Save(fileName, ImageFormat.Png);
                        //pageImage.Dispose();
                        AppConfig.ImageList.Add(pageImage);                        
                    }
                }
            }
            catch
            {
                //foreach(Bitmap bitmap in AppConfig.ImageList)
                //{
                //    bitmap.Dispose();
                //}
                return false;
            }
            
            return true;
        }

        public enum Definition
        {
            One = 1, Two = 2, Three = 3, Four = 4, Five = 5, Six = 6, Seven = 7, Eight = 8, Nine = 9, Ten = 10
        }
        
        //-----------------多图片转PDF-------------------
        public static bool images2pdf(List<Bitmap> images,string path)
        {
            // iTextSharp.text.Document document = new iTextSharp.text.Document(iTextSharp.text.PageSize.A4, 25, 25, 25, 25);
            iTextSharp.text.Document document = new iTextSharp.text.Document(iTextSharp.text.PageSize.A4);
            try
            {
                iTextSharp.text.pdf.PdfWriter.GetInstance(document, new FileStream(path, FileMode.Create, FileAccess.ReadWrite));
                document.Open();
                iTextSharp.text.Image image;

                for (int i = 0; i < images.Count(); i++)
                {                    
                    image = iTextSharp.text.Image.GetInstance(images[i], BaseColor.WHITE);

                    if (image.Height > iTextSharp.text.PageSize.A4.Height - 25)
                    {
                        image.ScaleToFit(iTextSharp.text.PageSize.A4.Width - 25, iTextSharp.text.PageSize.A4.Height - 25);
                    }
                    else if (image.Width > iTextSharp.text.PageSize.A4.Width - 25)
                    {
                        image.ScaleToFit(iTextSharp.text.PageSize.A4.Width - 25, iTextSharp.text.PageSize.A4.Height - 25);
                    }
                    image.Alignment = iTextSharp.text.Image.ALIGN_MIDDLE;
                    document.NewPage();
                    document.Add(image);
                }
                return true;
            }
            catch (Exception ex)
            {
                return false;
            }
            document.Close();
            Console.ReadKey();
        }
    }
}
